<html>

<head>
	<title>组织架构</title>
	<meta http-equiv="Content-Type" content="text/html; charset=UTF-8">
	<link href="../../build/common/base.css" rel="stylesheet">
	<script type="text/javascript" src="../../build/common/base.js"></script>
	<script type="text/javascript" src="../../build/common/ngEdit.js"></script>
	<script type="text/javascript" src="../../build/common/easyLayout.js"></script>
</head>

<body class="easy-layout">
	<div data-options="region:'west',title:'组织管理',split:true" style="width:200px;">
		<div id="orgTree" class="ztree"></div>
		<div class="hidden">
			<div id="orgMenu" class="easyui-menu" data-options="onClick:menuHandler" style="width: 120px;">
				<div data-options="name:'add'">添加子级组织</div>
				<div data-options="name:'edit'">编辑当前组织</div>
				<div data-options="name:'remove'">删除当前组织</div>
				<div data-options="name:'fresh'">刷新</div>
			</div>
		</div>
	</div>
	<div data-options="region:'center',border:false" style="text-align: center;">
		<iframe id="listFrame" src="" frameborder="no" width="100%" height="100%"></iframe>
	</div>
</body>

</html>
<script>
	//加载树
	var orgTree;
	function loadTree() {
		var ztreeCreator = new ZtreeCreator('orgTree', __ctx + "/org/group/getTreeData")
			.setCallback({ onClick: zTreeOnLeftClick, onRightClick: zTreeOnRightClick })
			.initZtree({}, function (treeObj) { orgTree = treeObj });
	};

	//左击
	function zTreeOnLeftClick(event, treeId, treeNode) {
		curSelectNode = treeNode;
		var parentId = treeNode.id;
		var url = "groupTabs.html?id=" + curSelectNode.id;
		if (curSelectNode.id == "0") {
			$.Toast.error("该节点为根节点，请点击具体的组织");
			return false;
		}
		$("#listFrame").attr("src", url);
	};
	/**
	 * 树右击事件
	 */
	function zTreeOnRightClick(e, treeId, treeNode) {
		if (treeNode) {
			orgTree.selectNode(treeNode);
			curSelectNode = treeNode;
			var isfolder = treeNode.isFolder;
			var h = $(window).height();
			var w = $(window).width();
			var menuWidth = 120;
			var menuHeight = 75;
			var menu = null;
			if (treeNode != null) {
				menu = $('#orgMenu');
			}
			var x = e.pageX, y = e.pageY;
			if (e.pageY + menuHeight > h) {
				y = e.pageY - menuHeight;
			}
			if (e.pageX + menuWidth > w) {
				x = e.pageX - menuWidth;
			}
			menu.menu('show', {
				left: x,
				top: y
			});
		}
	};
	//菜单对应项
	function menuHandler(item) {
		if ('add' == item.name) {
			editNode(0);
		} else if ('remove' == item.name) {
			delNode();
		} else if ('sort' == item.name) {
			sortNode();
		} else if ('edit' == item.name) {
			editNode(1);
		} else if ('fresh' == item.name) {
			refreshNode();
		}
	};
	function refreshNode() {
		loadTree();
	};
	//添加节点
	function editNode(type) {
		var selectNode = getSelectNode();
		if (!selectNode) return;
		var id = selectNode.id;
		var url = "./groupEdit.html";
		url += (type == 1) ? "?id=" + id : "?parentId=" + id + "&parentOrgName=" + selectNode.name;
		$("#listFrame").attr("src", url);

	};


	//删除
	function delNode() {
		var selectNode = getSelectNode();
		var nodeId = selectNode.id;
		if (nodeId == "0") {
			$.topCall.warn('该节点为根节点，不可删除');
			return;
		}

		var callback = function () {
			var url = __ctx + "/org/group/remove";
			var params = {
				id: nodeId
			};
			$.post(url, params, function (data) {
				var obj = JSON.parse(data);
				if (obj.isOk) {//成功
					orgTree.removeNode(selectNode);
					$("#listFrame").attr("src", "about:blank");
				} else {
					$.Dialog.error(obj.msg);
				}
				refreshNode();
			});
		};
		$.Dialog.confirm('温馨提示', '删除组织会级联删除下属组织，确定删除？', callback);
	};
	//选择资源节点。
	function getSelectNode() {
		orgTree = $.fn.zTree.getZTreeObj("orgTree");
		var nodes = orgTree.getSelectedNodes();
		var node = nodes[0];
		return node;
	};
</script>
<script>
	$(function () {
		loadTree();
	});

</script>